import clr, sys

clr.AddReferenceByPartialName("Microsoft.Office.Interop.Outlook")
clr.AddReferenceByPartialName("office")

from Microsoft.Office.Interop import Outlook
from Microsoft import Office

from System.Reflection import *

from MailHandler import *
from ActiveXObject import *
from GtdUi import *
from Globals import *

class ExplorerUi(GtdUi):
    def __init__(self, explorer, parent, attachOnActivate):
        GtdUi.__init__(self, explorer, parent, attachOnActivate)
        
        self.explorer = explorer

        # Workaround to add the event handler.
        Outlook.ExplorerEvents_10_Event.add_Close(explorer, self.OnClose)

        if not attachOnActivate:
            # Attach immediately
            self.Attach()
        else:
            Outlook.ExplorerEvents_10_Event.add_Activate(explorer, self.OnActivate)

    def AddGtdControls(self):
        self.AddCommandBarButton("&-Archieve", 0, "Archive selected items", \
                                 lambda ctrl, cancelDefault: self.OnClick(ArchiveMailItem))
        self.AddCommandBarButton("&=Defer", 0, "Will this take me longer than two minutes?", \
                                 lambda ctrl, cancelDefault: self.OnClick(Defer))
        self.AddCommandBarButton("&*Action", 0, "Can I resolve this email in less than two minutes?", \
                                 lambda ctrl, cancelDefault: self.OnClick(NextAction))
        
    def OnClick(self, func):
        try:
            for item in self.explorer.Selection:
                if "MailItem" == GetComObjectTypeName(item):
                    func(ActiveXObject(item))
        except:
            ReportException()
